//
//  home.swift
//  WuZhen
//
//  Created by 今安在 on 2024/3/25.
//

import SwiftUI
import UIKit

struct home: View {
    
    var body: some View {
        NavigationView{
            ScrollView {
                
                TabView{
                    TabViewContent(photoName: "IMG_8975",
                                   textTitle: "度假",
                                   textContent: "推窗而出，或许就与小桥流水的江南气息撞个满怀。")
                    TabViewContent(photoName: "IMG_8972",
                                   textTitle: "文化",
                                   textContent: "传统碰撞现代，在乌镇感受中国古老文化的非凡魅力和现代东方生活的融合")
                    TabViewContent(photoName: "2020051514373412757",
                                   textTitle: "木心美术馆",
                                   textContent: "应家乡乌镇竭诚邀请，于2006年回乡定居")
                    TabViewContent(photoName: "2023061116535140257",
                                   textTitle: "乌镇戏剧节",
                                   textContent: "十年归心，玉汝于成，戏剧与生活相遇在这个江南小镇，缔造一场属于所有戏剧爱好者与生活梦想家的烟雨大梦。")
                }
                .tabViewStyle(PageTabViewStyle())
                .frame(height: 300)
                
                //icons
                VStack(spacing: 25) {
                    
                    HStack(spacing: 20) {
                        Spacer()
                        ButtonView(systemNameForImage: "creditcard", textContent: "景区购票",isWeb: true,urlName: "https://m.ewuzhen.com/combo/list")
                        ButtonView(systemNameForImage: "bed.double", textContent: "住宿预定",isWeb: true,urlName: "https://m.ewuzhen.com/hotel/list")
                        ButtonView(systemNameForImage: "gift", textContent: "特惠购物",isWeb: true,urlName: "https://www.ewuzhen.com/goods/list")
                        ButtonView(systemNameForImage: "graduationcap", textContent: "达人招募",isWeb: true,urlName: "https://wj.qq.com/s2/7252513/6d77")
                        ButtonView(systemNameForImage: "shippingbox", textContent: "失物招领",isWeb: true,urlName: "http://www.wuzhen.com.cn/web/traver/server?id=24")
                        Spacer()
                    }
                    HStack(spacing: 20) {
                        Spacer()
                        ButtonView(systemNameForImage: "fork.knife", textContent: "乌镇美食",isWeb: true,urlName: "http://www.wuzhen.com.cn/web/food?id=4")
                        ButtonView(systemNameForImage: "person.2", textContent: "乌镇名人",isWeb: true,urlName: "http://www.wuzhen.com.cn/web/index/celebrity?id=1")
                        ButtonView(systemNameForImage: "book.pages", textContent: "游记攻略",isWeb: true,urlName: "http://www.wuzhen.com.cn/web/traver/tour")
                        ButtonView(systemNameForImage: "balloon", textContent: "精彩活动",isWeb: true,urlName: "http://www.wuzhen.com.cn/web/cultrue?id=5")
                        ButtonView(systemNameForImage: "car", textContent: "周边交通",isWeb: true,urlName: "http://www.wuzhen.com.cn/web/traver/scenic?id=25")
                        Spacer()
                    }
                    .padding(.top, -15)
                }
                .padding()
                
                handbooksSection
                    .padding(.horizontal)
                    
            }
            .navigationTitle("首页")
            .navigationBarItems(trailing:HStack(spacing: 10) {ButtonUp1();ButtonUp2()}.padding())
            }
        }
    
    var handbooksSection: some View {
        HStack(alignment: .top, spacing: 16) {
            ForEach(handbooks) { handbook in
                HandbookItem(handbook: handbook)
                    .cornerRadius(30)
            }
        }
        .padding(.horizontal, 20)
    }
}

#Preview {
    home()
}

struct ButtonView: View {
    @State var systemNameForImage: String
    @State var textContent: String
    @State var isWeb : Bool
    @State var urlName : String
    
    var body: some View {
        
        Link(destination: URL(string: urlName)!){
            VStack(spacing:10) {
                
                Circle()
                    .fill(LinearGradient(colors: [Color.blue, Color.green], startPoint: .topLeading, endPoint: .bottomTrailing))
                    .frame(width: 55, height: 55)
                    .overlay(
                        Image(systemName: systemNameForImage)
                            .font(.system(size: 30))
                            .foregroundColor(.white)
                    )
                
                Text(textContent)
                    .font(.system(size: 12))
                    .foregroundColor(.primary)
                    .lineLimit(1)
            }
        }
        .ignoresSafeArea()
    }
       
}

struct ButtonUp1:View {
    @State var showSheet :Bool = false
    var body: some View {
        Button(action: {
            showSheet.toggle()
        }, label: {
            Image(systemName: "doc.text.magnifyingglass")
                .font(.system(size: 12, weight: .bold))
                .frame(width: 30, height: 30)
                .foregroundColor(.secondary)
                .background(.ultraThinMaterial)
                .cornerRadius(10)
                .shadow(radius: 5)
        })
        .sheet(isPresented: $showSheet, content: {
            SearchView()
        })
    }
}

struct ButtonUp2:View {
    @State var showSheet :Bool = false
    var body: some View {
        Button(action: {
            showSheet.toggle()
        }, label: {
            Image(systemName: "person.badge.key")
                .font(.system(size: 12, weight: .bold))
                .frame(width: 30, height: 30)
                .foregroundColor(.secondary)
                .background(.ultraThinMaterial)
                .cornerRadius(10)
                .shadow(radius: 5)
        })
        .sheet(isPresented: $showSheet, content: {
            LogInView()
        })
    }
}

struct TabViewContent: View {
    @State var photoName : String
    @State var textTitle : String
    @State var textContent : String
    
    var body: some View {
            ZStack(alignment: .bottom) {
                GeometryReader { geometry in
                    Image(photoName)
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: geometry.size.width, height: geometry.size.height)
                        .clipped()
                    
                    VStack {
                        Spacer()
                        VStack(alignment: .leading) {
                            Text(textTitle)
                                .font(.headline)
                            Text(textContent)
                                .font(.callout)
                        }
                        .foregroundColor(.white)
                        .padding()
                        .background(.ultraThinMaterial)
                        .cornerRadius(10)
                        .frame(width: geometry.size.width)

                    }
                }
            }
    }
}
